angular.module('shopCartModule', [])
    .controller('shopCartCtrl', ['$scope','$http','$location',function($scope,$http,$location) {
        
        $scope.shopCartDatas = [];
        $scope.shopCartDatasNum = 0;
        $http.get('./admin/shopCart.php?getType=getShopcart')
            .then(function(res){
                var data = resetShopCart(res.data)
                $scope.shopCartDatasNum = data.length;
                if(res.data && res.data.length > 0){
                    $scope.shopCartDatas = data;
                }
            });

        $scope.add = function(index){
            $scope.shopCartDatas[index].num++;
        };

        $scope.reduce = function(index){
            if($scope.shopCartDatas[index].num > 1){
                $scope.shopCartDatas[index].num--;
            }
        };

        $scope.getTotalPrice = function(){
            var total = 0;
            angular.forEach($scope.shopCartDatas,function(item){
                if(item.isCheck){
                    total += item.price * item.num;
                }
            })
            return total.toFixed(2);
        }

        $scope.isCheckAll = function(){
            var isAll = true;
            angular.forEach($scope.shopCartDatas,function(item){
                if(!item.isCheck){
                    isAll = false;
                }
            })
            return isAll;
        }

        $scope.setCheck = function(){
            if($scope.isCheckAll()){
                angular.forEach($scope.shopCartDatas,function(item){
                    item.isCheck = false;
                });
            }else{
                angular.forEach($scope.shopCartDatas,function(item){
                    item.isCheck = true;
                });
            }
        }

        $scope.changeNum = function(index){
            $http.post('./admin/shopCart.php',{
                postType :'changeNum',
                id : $scope.shopCartDatas[index].shopcart_id,
                num : $scope.shopCartDatas[index].num
            }).then(function(res){
                console.log(res.data);
            })
        }

        $scope.submit = function(){
            var idArr = [];
            var idStr = '';
            angular.forEach($scope.shopCartDatas,function(item){
                if(item.isCheck ==true ){
                    idArr.push(item.shopcart_id);
                }
            });
            idStr = idArr[0];
            for(var i=1;i<idArr.length;i++){
                idStr = idStr+','+idArr[i];
            }
            idStr = "("+idStr+")";
            console.log(idStr);
            $http.post('./admin/shopCart.php',{
                postType :'submit',
                idStr : idStr
            }).then(function(res){
                if(res.data.status == 1){
                    //$location.path("confirmOrder");
                }else{
                    alert("错误");
                }
            })
        }

        function resetShopCart(data){
            angular.forEach(data,function(item){
                item.isCheck = true;
            });
            return data;
        };



    }]);

